package com.hangwei.paperless;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hangwei.paperless.adapter.EngTreatmentAdapter;
import com.hangwei.paperless.model.bean.EngTreatmentInfo;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.widget.MyTextWatcher;
import com.twelve.all.base.BaseActivity;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;

import java.util.List;

/**
 * 工程处理
 */
public class EngTreatmentActivity extends BaseActivity {

    private String woNO = "";
    private LoadingDialog mDialog;
    private String mUserId;
    private EngTreatmentAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eng_treatment);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new EngTreatmentAdapter(null);
        recyclerView.setAdapter(mAdapter);
        mUserId = SPUtil.getString(SPKey.Account.USER_ID);
        EditText etNum = findViewById(R.id.etNum);
        TextView tvSearch = findViewById(R.id.tvSearch);
        etNum.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                woNO = s.toString().trim();
            }
        });
        tvSearch.setOnClickListener(v -> initData(false));
        initData(true);
    }

    private void initData(boolean firstLoad) {
        if (mDialog == null)
            mDialog = new LoadingDialog(this);
        if (!mDialog.isShowing())
            mDialog.show();
//        if (firstLoad) {
//        }
        StaticPresenter.getEngineerDispositionData(mUserId,woNO, new CallBack<List<EngTreatmentInfo>>() {
            @Override
            public void onCompleted(List<EngTreatmentInfo> engTreatmentAll) {
                if (mDialog!=null&&mDialog.isShowing())mDialog.dismiss();
                mAdapter.setNewData(engTreatmentAll);
            }

            @Override
            public void onError() {
                if (mDialog!=null&&mDialog.isShowing())mDialog.dismiss();
            }
        });
    }
}
